package com.kgc.pay.service.impl;

import com.github.wxpay.sdk.WXPayUtil;
import com.kgc.pay.service.PayService;
import com.kgc.pay.utils.HttpClient;
import com.kgc.pay.utils.PayCode;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class PayServiceImpl implements PayService {
	//生成二维码,参数1表示订单号,参数2表示金额

	public Map generaterQRious(String out_trade_no, String total_fee) {
		try{
			//1.参数封装
			Map<String, String> map=new HashMap();
			map.put("appid", PayCode.appid);//应用id
			map.put("mch_id",PayCode.partner);//商户号
			map.put("nonce_str",WXPayUtil.generateNonceStr());//随机码

			map.put("body", "微信支付测试");//商品描述
			map.put("out_trade_no", out_trade_no);//商户订单号
			map.put("total_fee", total_fee);//标价金额
			map.put("spbill_create_ip", "127.0.0.1");//终端IP
			map.put("notify_url", PayCode.notifyurl);//异步通知地址
			map.put("trade_type", "NATIVE");//交易类型
			//2.发送请求
			//生成xml类型
			String xmlMap= WXPayUtil.generateSignedXml(map, PayCode.partnerkey);
			//请求类
			HttpClient httpClient=new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
			httpClient.setHttps(true);//是否遵循https协议
			httpClient.setXmlParam(xmlMap);//添加xml数据
			httpClient.post();//post请求发送
			//3.获取返回结果
			String xmlResult = httpClient.getContent();
			//把获取的xml转化成map类型
			Map<String, String> xmlToMap = WXPayUtil.xmlToMap(xmlResult);
			System.out.println(xmlToMap);
			Map<String, String>payMap=new HashMap();
			//生成支付二维得所需要的数据
			payMap.put("code_url", xmlToMap.get("code_url"));
			payMap.put("total_fee", total_fee);
			payMap.put("out_trade_no", out_trade_no);
			return payMap;
		} catch (Exception e) {
			return new HashMap();
		}

	}
	//根据订单号查询订单
	public Map queryPayStatus(String out_trade_no) {
		//1.参数封装
		Map<String, String> paramMap=new HashMap();
		paramMap.put("appid", PayCode.appid);//应用id
		paramMap.put("mch_id", PayCode.partner);//商户号
		paramMap.put("out_trade_no", out_trade_no);//商户订单号
		paramMap.put("nonce_str", WXPayUtil.generateNonceStr());//随机码
		//2.发送请求
		try {
			//转化成xml类型
			String xmlParam = WXPayUtil.generateSignedXml(paramMap, PayCode.partnerkey);
			HttpClient httpClient=new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
			httpClient.setHttps(true);
			httpClient.setXmlParam(xmlParam);
			httpClient.post();
			//3.返回结果
			String xmlResult = httpClient.getContent();
			Map<String, String> map = WXPayUtil.xmlToMap(xmlResult);
			return map;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}

}
